package com.tanhua.server.controller;

import com.tanhua.model.domain.Settings;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.server.service.UserSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author げっしょく
 * @date 2021/10/24
 */

@RestController
@RequestMapping("/users")
public class SettingController {

    @Autowired
    private UserSettingService userSettingService;

    /**
     * 查询我的通用设置
     */
    @GetMapping("/settings")
    public ResponseEntity settings() {
        SettingsVo settingsVo = userSettingService.getSettings();
        return ResponseEntity.ok(settingsVo);
    }

    /**
     * 设置或更新用户陌生人问题
     * @param content 用户需要设置的陌生人问题
     * @return
     */
    @PostMapping("/questions")
    public ResponseEntity questions(@RequestBody Map content) {
        String content1 = (String) content.get("content");
        userSettingService.setquestions(content1);
        return ResponseEntity.ok(null);
    }

    /**
     * 用户修改通知设置
     * @param settings 用户设置的通知设置
     * @return
     *
     */
    @PostMapping("/notifications/setting")
    public ResponseEntity setting(@RequestBody Settings settings) {
        userSettingService.setSettings(settings);
        return ResponseEntity.ok(null);
    }

    /**
     * 查询当前用户的黑名单列表
     * @param page 第几页
     * @param pagesize 每页几条数据
     * @return
     */
    @GetMapping("blacklist")
    public ResponseEntity blacklist(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = userSettingService.blacklist(page, pagesize);

        return ResponseEntity.ok(pageResult);
    }

    /**
     * 取消黑名单用户
     * @param id 需要被取消黑名单的用户id
     * @return
     */
    @DeleteMapping("/blacklist/{id}")
    public ResponseEntity blacklist(@PathVariable Long id) {
        userSettingService.delectBlack(id);
        return ResponseEntity.ok(null);
    }

    /**
     * 修改手机号：发送验证码
     * @return
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode() {
        userSettingService.updatephoneSendVerCode();
        return ResponseEntity.ok(null);
    }

    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map) {
        String code = (String) map.get("verificationCode");
        Map map1 = userSettingService.checkVerificationCode(code);

        return ResponseEntity.ok(map1);
    }


    /**
     *  修改手机号：用户需要修改成的手机号
     * @param map 用户需要修改成的手机号
     * @return
     */
    @PostMapping("/phone")
    public ResponseEntity phone(@RequestBody Map map) {
        String phone = (String) map.get("phone");
        userSettingService.updatephone(phone);
        return ResponseEntity.ok(null);
    }

}
